package com.tianya.controller;

import com.tianya.common.Results;
import com.tianya.domain.dto.LeaveRequestDTO;
import com.tianya.domain.entity.Leave;
import com.tianya.domain.vo.LeaveVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.service.LeaveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author TianYaGuoKe
 * @date 2025/5/26 23:17
 * @description LeaveController
 */
@Slf4j
@Api("请假管理")
@RestController
@RequestMapping("/leave")
public class LeaveController {

    @Resource
    private LeaveService leaveService;

    @ApiOperation("创建请假")
    @PostMapping
    public Results createLeave(@RequestBody LeaveRequestDTO leaveRequestDTO) {
        leaveService.createLeave(leaveRequestDTO);
        return Results.success();
    }

    @ApiOperation("分页获取请假")
    @PostMapping("/page")
    public Results<PageResultVO<LeaveVO>> pageLeave(@RequestParam(defaultValue = "1") Integer pageNum,
                                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                                    LeaveRequestDTO leaveRequestDTO) {
        log.info("分页获取请假参数！！！！！：{}", leaveRequestDTO);
        return Results.success(leaveService.pageLeave(pageNum, pageSize, leaveRequestDTO));
    }

    @ApiOperation("获取请假")
    @GetMapping
    public Results<Leave> getLeave(@RequestParam Long id) {
        return Results.success(leaveService.getById(id));
    }

    @ApiOperation("更新请假")
    @PutMapping
    public Results updateLeave(@RequestBody Leave leave) {
        leaveService.updateById(leave);
        return Results.success();
    }

    @ApiOperation("删除请假")
    @DeleteMapping("/{id}")
    public Results deleteLeave(@PathVariable Long id) {
        leaveService.removeById(id);
        return Results.success();
    }
}
